package com.ruoyi.web.wuchuan.command;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.web.wuchuan.dto.PDFProperties;
import com.ruoyi.web.wuchuan.dto.TzhqytxDTO;
import com.ruoyi.web.wuchuan.enmu.Param;
import com.ruoyi.web.wuchuan.handle.PDFModule;
import com.ruoyi.web.wuchuan.util.PDFutil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AnalysisSuitable implements PDFModule {
    private static final String pdfName = "宜灌木区域分析报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".pdf";
    private static final String tempPath = System.getProperty("java.io.tmpdir");
    private final String zipName = "宜灌木区域分析报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";


    @Override
    public String getZipName() {
        return zipName;
    }

    @Override
    public String analyticalDataByType(PDFProperties pdfProperties, String uuid) {

        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempPath+pdfName));
            document.addTitle("example of PDF");
            document.open();
            document.add(CretePDF(pdfProperties,uuid));
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return tempPath+pdfName;
    }
    private PdfPTable CretePDF(PDFProperties pdfProperties,String uuid){
        PdfPTable table = new PdfPTable(Param.tittleSzls);
//        table.setSpacingBefore(20f);//设置页边距
//        table.setWidthPercentage(100);//设置表格宽度为100%
//        table.getDefaultCell().setMinimumHeight(30);
        PDFutil pdFutil = new PDFutil();
        //一级标题
        table.addCell(pdFutil.getCell("宜灌木区域分析报告", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 1));
        //生成key
        table.addCell(pdFutil.getCell("总面积",pdFutil.getFont(Param.clofontSize,true),Param.cloSzls,Param.cloHigh,0));
        table.addCell(pdFutil.getCell(pdfProperties.getBaseData().getMj(),pdFutil.getFont(Param.clofontSize,false),Param.cloSzls,Param.cloHigh,0));
        table.addCell(pdFutil.getCell("包含功能区数量",pdFutil.getFont(Param.clofontSize,true),Param.cloSzls,Param.cloHigh,0));
        table.addCell(pdFutil.getCell(pdfProperties.getBaseData().getSl(),pdFutil.getFont(Param.clofontSize,false),Param.cloSzls,Param.cloHigh,0));
        table.addCell(pdFutil.getCell("同质化区域", pdFutil.getFont(Param.tittleFontSize, true), Param.cloSzls*4, Param.cloHigh, 0)); 
        table.addCell(pdFutil.getCell("区域行政区",pdFutil.getFont(Param.clofontSize,true),Param.cloSzls * 2,Param.cloHigh,0));
        table.addCell(pdFutil.getCell("行政区代码",pdFutil.getFont(Param.clofontSize,true),Param.cloSzls,Param.cloHigh,0));
        table.addCell(pdFutil.getCell("面积(平方米)",pdFutil.getFont(Param.clofontSize,true),Param.cloSzls,Param.cloHigh,0));
        List<TzhqytxDTO> tzhqytxDTOs = pdfProperties.getTzhqy();
        tzhqytxDTOs.forEach(tzhqytxDTO ->{
            table.addCell(pdFutil.getCell(tzhqytxDTO.getXzqmc(),pdFutil.getFont(Param.clofontSize,false),Param.cloSzls*2,Param.cloHigh,0));
            table.addCell(pdFutil.getCell(tzhqytxDTO.getXzqdm(),pdFutil.getFont(Param.clofontSize,false),Param.cloSzls,Param.cloHigh,0));
            table.addCell(pdFutil.getCell(tzhqytxDTO.getMj(),pdFutil.getFont(Param.clofontSize,false),Param.cloSzls,Param.cloHigh,0));
        });
        return table;
    }

}
